
import { _decorator, Component, Label, Node, instantiate, EditBox, Button, tween, view, Tween, Vec3, find, Sprite } from 'cc';
import { App } from '../../App';
import { EventConst } from '../../common/Common';
import CommConst from '../../const/CommConst';
import wsTool from '../../net/ws';
import { PD } from '../../player/PlayerData';
import { showTip } from '../../tools/PopuUp';
import { ShowTip } from '../../tools/ShowTip';
import UtilTool from '../../tools/UtilTool';
const { ccclass, property } = _decorator;

/* 下注界面 */
@ccclass('LBBetPanel')
export class LBBetPanel extends Component {
    @property(Node)
    contentNode: Node = null;   // 下注

    private parentObj = null
    private ballNumber:number = null // 下注的球号码

    // 下注数据
    private iconEditBox:EditBox = null
    private iconNum = null   // 输入下注数

    private betNums = [100,500,1000,5000]

    protected onLoad(): void {
        App.Event.on(EventConst.UPDATE_COIN,this.setIcon, this )
        
    }

    start() {
       UtilTool.setScreen(this.node)
       UtilTool.nodeActive(this.contentNode,5,0.16)

       this.setIcon()
    }

    update(deltaTime: number) {
        
    }

    /**
     * 更新金币
     */
    setIcon() {
        if (this.node==null) {
            return
        }
        var icon = PD.user.coin
        let myIcon = this.contentNode.getChildByName("myCoin")
        myIcon.getComponent(Label).string = App.language.getLanguageLab(25) + icon
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////// 下注界面 /////////////////////////////////////////////////////////////////////////////////////////
    /** 加载下注界面
     * @param obj 父类
     * @param number 球号码
      */
    setData(obj ,number) {
        var that = this
        this.parentObj = obj
        this.ballNumber = number

        let ball = find("ballbox/ball",this.contentNode)
        ball.getComponent(Sprite).spriteFrame = this.parentObj.ballArr[number]

        // 输入框
        this.iconEditBox = this.contentNode.getChildByName("editboxBg").getChildByName("iconEditBox").getComponent(EditBox)
        this.iconEditBox.inputMode = EditBox.InputMode.NUMERIC;
        this.iconEditBox.node.on('editing-did-ended',(e)=>{
            UtilTool.printLog(e,e.string,'e'); 
            this.iconNum = parseInt(e.string);
            UtilTool.printLog(this.iconNum,e.string,'num');
        })

        // 档位选择
        for (let index = 1; index <= 4; index++) {
            var tag = this.contentNode.getChildByName("tagbg" + index)
            var coinLabel = tag.getChildByName("coinLabel")
            var coinSpr = tag.getChildByName("coinSpr")
            var usdtLabel = tag.getChildByName("usdtLabel")

            // coinLabel.getComponent(Label).string = UtilTool.formatValue(this.betNums[index-1],{digit:0,type:2})
            usdtLabel.getComponent(Label).string = "≈" + this.betNums[index-1]/PD.gameConfig.chargeScale + " " + PD.user.usdt
            UtilTool.adaptiveLabel([coinSpr,coinLabel], 0, 10)

            App.onClick.addButtonClickEvent(tag.getComponent(Button), async(param) =>{
                that.setBetNumber(that.betNums[param-1])
            }, that,index,true,null,null,100);
        }

        // 投注限制
        let limitLabel = find("limitLabel",this.contentNode).getComponent(Label)
        limitLabel.string = App.language.getLanguageLab(28) + PD.gameConfig.userCurrentOrderMinLimit + "-" + PD.gameConfig.userCurrentOrderMaxLimit
    }

    // 设置下注的数量
    setBetNumber(count:number) {
        this.iconNum = count
        this.iconEditBox.string = count.toString()
    }

    // 设置最大下注的数量
    setMaxBetNumber() {
        this.iconNum = PD.gameConfig.userCurrentOrderMaxLimit
        this.iconEditBox.string = this.iconNum.toString()
    }

    /** 确定
     * @param data
      */
    clickSure() {
        let that = this
        App.Audio.playOneShot("click")

        if (this.iconNum==null) {
            showTip(App.language.getLanguageLab(29))
            return
        }
        if (this.iconNum==0) {
            showTip(App.language.getLanguageLab(29))
            return
        }
        if (this.iconNum>PD.user.coin) {
            showTip(App.language.getLanguageLab(30))
            return
        }
        if (this.iconNum<PD.gameConfig.userCurrentOrderMinLimit || this.iconNum>PD.gameConfig.userCurrentOrderMaxLimit) {
            showTip(App.language.getLanguageLab(31))
            return
        }

        // 请求下单
        wsTool.send({"msgCode":CommConst.Req_BET,"value":this.iconNum,"type":this.ballNumber})
        .then(res =>{
            if (res?.result == 0) {
                UtilTool.printLog("下注成功:",res)
                App.Event.emit(EventConst.UPDATE_MODE1_BET, {
                    id: res.id,
                    nums: this.ballNumber.toString(),
                    value: this.iconNum,
                })

                if (that.node) {
                    that.node.destroy();
                }
            }
            else if (res?.result == 308){   // 购买总值已经超过10w
                showTip(App.language.getLanguageLab(42))
            }
            else {
                showTip(App.language.getLanguageLab(43))
            }
        }) 
    }

    /** 
     * 关闭
      */
    clickClose() {
        App.Audio.playOneShot("click")
        this.node.destroy();
    }

    onDestroy() {
        App.Event.off(EventConst.UPDATE_COIN,this.setIcon,this)
    }

}

